//
// Created by tang on 2022/1/21.
//

#include "credits_view.hpp"
#include "gui/controls/custom_controls.hpp"
#include "gui/dialog_names.h"
#include "gui/ui_component.hpp"
#include "imgui/imgui.h"
#include "imgui/IconsFontAwesome4.h"
#include "app/version.h"
#include "app/app_base.hpp"
#include "sql/types.hpp"

#include <fmt/format.h>

struct FreeSoftwareInfo {
    const char* name;
    const char* website;
    const char* license;
    const char* help;
};

constexpr FreeSoftwareInfo used_libraries[]{
        {"glfw","https://www.glfw.org/","Zlib","跨平台的OpenGL/Vulkan窗口创建库"},
        {"imgui","https://github.com/ocornut/imgui","MIT","Dear ImGui is a bloat-free graphical user interface library for C++"},
        {"implot","https://github.com/epezent/implot","MIT","imgui的绘图扩展"},
        {"curl","https://curl.se/","MIT","C语言编写的网络库，包含许多协议（如HTTP、FTP等）"},
        {"cpr","https://github.com/libcpr/cpr","MIT", "libcurl的C++封装"},
        {"json","https://json.nlohmann.me/","MIT","JSON for Modern C++"},
        {"fmt","https://fmt.dev","MIT", "C++格式化库"},
        {"EventBus","https://github.com/gelldur/EventBus","Apache-2.0", "C++17事件框架"},
        {"asio","http://think-async.com/Asio/","Boost Software License", "异步框架"},
        {"sqlite","https://github.com/mackyle/sqlite","Public Domain","轻量型数据库"},
        {"SqliteCpp","https://github.com/SRombauts/SQLiteCpp","MIT","SQLite的C++包装"},
        {"freetype","https://gitlab.freedesktop.org/freetype/freetype","GPLv2","字体渲染引擎"},
        {"IconFontCppHeaders","https://github.com/juliettef/IconFontCppHeaders","Zlib", "Font Awesome 的C++头文件定义"},
        {"Font Awesome 4 Font","https://fontawesome.com/","CC BY 4.0/SIL OFL 1.1","Web图标字体"},
        {"文泉驿微米黑","https://sourceforge.net/projects/wqy/","GPL", "中文字体"},
        {"Source Code Pro","https://github.com/adobe-fonts/source-code-pro","OFL-1.1", "英文字体"},
        {"quirrel","https://github.com/GaijinEntertainment/quirrel","MIT", "Quirrel - Gaijin Entertainment dynamic script language (former Squirrel)"},
        {"sqrat","https://github.com/GaijinEntertainment/sqrat","N/A", "Quirrel and Squirrel C++ binding library"},
        {"wren","https://github.com/wren-lang/wren","MIT", "Wren is a small, fast, class-based concurrent scripting language"},
        {"wrenbind17","https://github.com/matusnovak/wrenbind17","MIT", "A header only library for binding C++17 classes and functions to Wren"},
        {"native-file-dialog-extended","https://github.com/btzy/nativefiledialog-extended","Zlib", "About Cross platform (Windows, Mac, Linux) native file dialog library with C and C++ bindings, based on mlabbe/nativefiledialog."},
        {"angelscript","https://www.angelcode.com/angelscript","Zlib", "An extremely flexible cross-platform scripting library designed to allow applications to extend their functionality through external scripts."},
        {"nng","https://nng.nanomsg.org/","MIT", "nanomsg-next-generation -- light-weight brokerless messaging."},
};

const char* contributors[]{
    "Tang",
    "Yee",
};

unsigned int CreditsView::Flags() const {
    return eDialog;
}

void CreditsView::RenderContent(ComponentFlag flag) {
    using namespace ImGui;
    static auto data_version = App->Sql()->GetDataVersion();
    static std::string data_version_string
    = fmt::format("{}.{}.{}",
        VERSION_MAJOR_PART(data_version),
        VERSION_MINOR_PART(data_version),
        VERSION_REVISION_PART(data_version)
    );
    AlignTextToFramePadding();
    // if (BeginChild("##ContentRegion", ImVec2(0, -GetTextLineHeight() * 1.8f))) {
    Text("Diary Tool C++ - 一款实用日记工具，使用 C++ 语言开发。 版本：%s(%llx)。", get_version_string(), get_version_code());
    Text("数据版本：%s(%llx)",data_version_string.data(), data_version);
    Spacing();
    Text("项目地址：https://gitee.com/micro.tang/DiaryToolpp");
    SameLine();
    if (Button("复制"))
        SetClipboardText("https://gitee.com/micro.tang/DiaryToolpp");
    SeparatorText("开发人员名单");
    for (auto& x : contributors)
        BulletText("%s", x);
    Spacing();
    SeparatorText("使用到的开源软件列表");
    PushStyleVar(ImGuiStyleVar_ChildRounding, 0.f);
    PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.f);
    //if (BeginChild("##SoftwareList", ImVec2(0, -GetTextLineHeightWithSpacing() * 1.5f))) {
    if (BeginTable("##FSL", 3,
        ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit |
        ImGuiTableFlags_ScrollY | ImGuiTableFlags_NoHostExtendY,
        ImVec2(0, -GetTextLineHeightWithSpacing() * 1.5f)))
    {
        TableSetupColumn("软件名");
        TableSetupColumn("网站", ImGuiTableColumnFlags_WidthStretch);
        TableSetupColumn("许可协议");
        TableSetupScrollFreeze (0, 1); // pin header row
        TableHeadersRow();

        for (auto& x : used_libraries) {
            PushID(&x);
            TableNextRow();
            TableNextColumn();
            TextUnformatted(x.name);
            HelperMaker(x.help, true);
            TableNextColumn();
            AlignTextToFramePadding();
            if (Button(ICON_FA_CLIPBOARD)) {
                SetClipboardText(x.website);
            }
            SameLine();
            TextUnformatted(x.website);
            TableNextColumn();
            TextUnformatted(x.license);
            PopID();
        }
        EndTable();
    }
    //}
    //EndChild();
    PopStyleVar(2);
    // }
    // EndChild();

    WindowBottomButtons(1, default_ok_cancel_button_text, [](auto, auto) {
            CloseCurrentPopup();
        }, nullptr);
}

const char* CreditsView::GetName(ComponentFlag flag) {
    return MODAL_DIALOG_NAME(ABOUT_SOFTWARE_DIALOG_NAME);
}

ImVec2 CreditsView::DialogSize(ImVec2 viewportSize)
{
    return viewportSize * .95f;
}
